﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Xml.Linq;
using System.Linq;

namespace Hi_Baidu.ProcessModel
{
    partial class GetContextMenuViewModel : Model
    {
        /// <summary>
        ///     XML文件菜单地址
        /// </summary>
        private static String XML_MENU_FILE;

        /// <summary>
        ///     好友树上下文菜单
        /// </summary>
        /// <returns>
        ///     返回null , 则表示上下文菜单文件不存在
        /// </returns>
        public static ContextMenu GetFriendTreeViewContextMenu(String XML_CONTENTMENU_FILE_DIR)
        {
            //检测文件是否存在
            if (File.Exists(XML_CONTENTMENU_FILE_DIR))
            {
                return INNER_CONTEXTMENU_FACTORY(XML_CONTENTMENU_FILE_DIR);
            }
            return null;
        }

        /// <summary>
        ///     内部上下文菜单构造工厂
        /// </summary>
        /// <param name="XML_DIR" type="string">
        ///     <para>
        ///         上先问菜单文件地址
        ///     </para>
        /// </param>
        private static ContextMenu INNER_CONTEXTMENU_FACTORY(String XML_DIR)
        {
            ///////////////////////////////////////////////////////////////////////////////
            ///              读取并创建右键上下文菜单
            ///////////////////////////////////////////////////////////////////////////////
            XDocument adList = XDocument.Load(XML_DIR);
            var CM_FRIEND_TREEVIEW = from cm_FRIEND_TREEVIEW
                                         in adList.Descendants("Menu").Elements("MenuItem")
                                     select cm_FRIEND_TREEVIEW;
            //创建顶层菜单
            ContextMenu TOP_CONTEXTMENU = new ContextMenu();
            //开始遍历菜单
            String ItemText = "";
            foreach (var item in CM_FRIEND_TREEVIEW)
            {
                MenuItem menuItem = new MenuItem();
                //检测可用性
                menuItem.IsEnabled = bool.Parse(item.Attribute("ENABLE").Value);
                menuItem.Tag = item.Attribute("FUNCTION").Value;
                //检测可见性
                if (!bool.Parse(item.Attribute("VISIBLE").Value))
                    menuItem.Visibility = System.Windows.Visibility.Hidden;

                //判断是否拥有子元素
                if (item.HasElements)
                {
                    ItemText = item.Attribute("TEXT").Value;
                    if (ItemText == "-")
                    {
                        menuItem.Items.Add(new Separator());
                    }
                    else
                    {
                        menuItem.Header = ItemText;
                    }
                    TOP_CONTEXTMENU.Items.Add(menuItem);
                    foreach (var Inneritem in item.Nodes())
                    {
                        ItemText = ((XElement)Inneritem).Attribute("TEXT").Value;
                        if (ItemText == "-")
                        {
                            menuItem.Items.Add(new Separator());
                        }
                        else
                        {
                            MenuItem Inner_menuItem = new MenuItem();
                            Inner_menuItem.Header = ItemText;
                            Inner_menuItem.Tag = item.Attribute("FUNCTION").Value;
                            //检测可用性
                            Inner_menuItem.IsEnabled = bool.Parse(item.Attribute("ENABLE").Value);
                            //检测可见性
                            if (!bool.Parse(item.Attribute("VISIBLE").Value))
                                Inner_menuItem.Visibility = System.Windows.Visibility.Hidden;
                            menuItem.Items.Add(Inner_menuItem);
                        }
                    }
                }
                else
                {
                    ItemText = item.Attribute("TEXT").Value;
                    menuItem.Header = ItemText;
                    menuItem.Tag = item.Attribute("FUNCTION").Value;
                    if (ItemText == "-")
                    {
                        TOP_CONTEXTMENU.Items.Add(new Separator());
                    }
                    else
                    {
                        TOP_CONTEXTMENU.Items.Add(menuItem);
                    }
                }
            }
            TOP_CONTEXTMENU.AddHandler(ContextMenu.MouseDownEvent, (RoutedEventHandler) OnFriendTreeContextMenuClicked, true);
            return TOP_CONTEXTMENU;
        }
    }
}
